#! /usr/bin/python2

# damage.py
# damage files for testing purposes

# (C)Copyright 2013-2015 by Klaus-J. Wolf, kj (at) seismic (dot) de
# This file is licensed under the General Public License Version 2.
# http://www.gnu.org/licenses/gpl-2.0.html


import sys, mmap, random


if __name__=="__main__":
    
    if len(sys.argv)<3:
        
        #print "usage:",sys.argv[0],"<command>","file..."
        #print "command may be:"
        #print "\t-b<num>\t\tbit flips"
        #print "\t-B<num>\t\tbyte overwrites"
        #print "\t-D<num>,<size>\tarea deletions"
        #print "\t-I<num>,<size>\tarea insertions"
        print "usage:",sys.argv[0],"<number of bit flips>","<file>..."
        sys.exit(1)
    
    n = int(sys.argv[1])
    r = random.WichmannHill()
    
    for fn in sys.argv[2:]:
        
        f = open(fn, "r+b")
        mm = mmap.mmap(f.fileno(), 0)
        for i in range(n):
            p = r.randint(0, len(mm))
            mm[p] = chr((1<<r.randint(0,7)) ^ ord(mm[p]))
        mm.close()
        f.close()
